package com.shuoyue.ycgk.views.dialog.common;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.shuoyue.ycgk.R;


/**
 * 带确认取消按钮的Dialog
 * Created by Forty'7 on 2019/6/17.
 */

public class HintConfirmDialog extends Dialog {
    private Context mContext;
    private TextView tvTitel;
    private TextView tvContent;
    private TextView tvCancel;
    private TextView tvOk;

    public HintConfirmDialog(@NonNull Context context, String title, String content) {
        this(context, title, content, Gravity.CENTER);
    }

    public HintConfirmDialog(@NonNull Context context, String title, String content, int gravity) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_hint_confirm);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvTitel = findViewById(R.id.tv_title);
        tvTitel.setText(title);
        tvContent = findViewById(R.id.tv_content);
        tvContent.setText(Html.fromHtml(content));
        tvCancel = findViewById(R.id.tv_cancel);
        tvOk = findViewById(R.id.tv_ok);
        tvContent.setGravity(gravity);
        if (TextUtils.isEmpty(title)) {
            tvTitel.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(content)) {
            tvContent.setVisibility(View.GONE);
        }

        tvCancel.setOnClickListener(v -> dismiss());

        tvOk.setOnClickListener(v -> {
            tvOk.setEnabled(false);
            if (okLisenter != null) {
                okLisenter.ok();
            }
            dismiss();
            new Handler().postDelayed(() -> {
                if (tvOk != null) {
                    tvOk.setEnabled(true);
                }
            }, 2000);
        });


    }

    public void setButtonText(String left, String right) {
        if (tvCancel != null && !TextUtils.isEmpty(left)) {
            tvCancel.setText(left);
        }
        if (tvOk != null && !TextUtils.isEmpty(right)) {
            tvOk.setText(right);
        }
    }

    public void setCancelListener(View.OnClickListener listener) {
        if (tvCancel != null) tvCancel.setOnClickListener(listener);
    }

    public void setOkListener(View.OnClickListener listener) {
        if (tvOk != null) tvOk.setOnClickListener(listener);
    }

    public void setContentText(String contentText) {
        this.tvContent.setText(contentText);
    }

    public void setOkLisenter(OkLisenter okLisenter) {
        this.okLisenter = okLisenter;
    }

    OkLisenter okLisenter;

    public interface OkLisenter {
        void ok();
    }
}
